'use strict';
// 申请开通控制器模块
import {
    viewInit,
    selectCity,
    getCityparaments,
    checkedInput,
    showLoadingDialog,
    hideLoadingDialog
} from '../views/applyView.js';
var cw = require("../components/winLocation.js");
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var openid;
var $qyzch, $name, $phone, $road, $areaName, $applyBtn;

$(document).on("pageinit", "#applyPage", controllerInit);

// 控制器初始化
function controllerInit() {
    cw.checkBrowser();
    initValue();
    viewInit();
    getCityArray();
    $("#apply_btn").on("touchstart", applyOpen);
    $("input").on("input", function() {
        
        var cinputVal = checkedInput();
        if (cinputVal == "ok") {
            $applyBtn.attr("class", "weui-btn weui-btn_primary");
        } else {
            $applyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        }
    });
    $("input").on("change", function() {
        var thisVal = $(this).val();
        thisVal = thisVal.replace(/[^A-Za-z0-9_\u4e00-\u9fa5]/g, "");
        $(this).val(thisVal);
    });
}

// 申请开通
function applyOpen() {

    if ($applyBtn.hasClass("weui-btn_disabled")) {
        return;
    } else {
        showLoadingDialog();
        var ktParameters = getktParameters();
        var ierror = promptInputError(ktParameters);
        if (ierror) {
            return;
        } else {
            $.post(apply_URL, ktParameters, openback);
        }

    }

}


// 开通回调函数
function openback(odata) {
    hideLoadingDialog();
    console.dir(odata);
    if (odata.merpre_response && odata.merpre_response.resp_code == "0000") {
        var cgmsg = odata.merpre_response.resp_msg ? odata.merpre_response.resp_msg : "预约开通申请成功";
        errorObject.showErroDialog(cgmsg);
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    } else if (odata.merpre_response && odata.merpre_response.resp_code != "0000" && odata.merpre_response.resp_msg) {
        errorObject.showErroDialog(odata.merpre_response.resp_msg);
    } else if (odata.code != "0000" && odata.msg) {
        errorObject.showErroDialog(odata.msg);
    } else if (odata.error_response) {
        var errorMsg = "预约开通申请失败";
        if (odata.error_response.msg) {
            errorMsg = odata.error_response.msg;
            if (odata.error_response.code == "53") {
                setTimeout(function() {
                    location.href = LoginInit_URL;
                }, 3000);

            };
        } else if (odata.error_response.msg) {
            var errorMsg = odata.error_response.msg
        }

        errorObject.showErroDialog(errorMsg);
    } else {
        errorObject.showErroDialog("预约开通申请失败");
    }
}

// 提示
function promptInputError(ktParameters) {
    var ierror = false;
    if (ktParameters.registName.length < 6 || ktParameters.registName.length > 32) {
        ierror = true;
        errorObject.showErroDialog("请输入正确的企业工商注册名");
    } else if (ktParameters.relatName.length < 2 || ktParameters.relatName.length > 6) {
        ierror = true;
        errorObject.showErroDialog("请输入正确的联系人名字");
    } else if (ktParameters.ph_no.length < 11) {
        ierror = true;
        errorObject.showErroDialog("请输入正确的手机号");
    } else if (ktParameters.verifyCode.length != 4) {
        ierror = true;
        errorObject.showErroDialog("请输入正确的验证码");
    } else if (ktParameters.address.length > 30 || ktParameters.address.length < 2) {
        ierror = true;
        errorObject.showErroDialog("请输入正确的详细地址");
    }

    return ierror;
}

// 申请开通参数
function getktParameters() {
    var cityParameter = getCityparaments();
    var registName = $qyzch.val();
    var relatName = $name.val();
    var ph_no = $phone.val();
    var address = $road.val();
    var verifyCode = $("#verification_code").val();
    var provinceName = cityParameter.provinceAndCityName;
    window.province = cityParameter.locCity;

    var sbParameters = {
        "provinceName": provinceName,
        "registName": registName,
        "relatName": relatName,
        "ph_no": ph_no,
        "verifyCode": verifyCode,
        "address": address,
        "provinceName": provinceName,
        "province": province,
        "openid": openid
    }

    return sbParameters;
}

// 取省市请求
function getCityArray() {
    $.post(GetAreas_URL, {}, cityBack);
}

// 取得省市回调函数
function cityBack(cdata) {
    console.dir(cdata);
    var createselctCity = "0";
    $("#area_naem-box").on("touchstart", function() {
        setTimeout(function() {
            $('#cities-componet').fadeIn();
            $('.city_pop').css("bottom", "0");
            if (createselctCity == "0") {
                selectCity(cdata);
                createselctCity = "1";
            }

        }, 200);

    });
}

// 初始化变量
function initValue() {
    cw.QueryString.Initial();
    $qyzch = $("#qyzch");
    $name = $("#name");
    $phone = $("#phone");
    $road = $("#road");
    $areaName = $("#area_name");
    $applyBtn = $("#apply_btn");
    openid = cw.QueryString.GetValue("openid");
    window.openid = openid;
    window.errorObject = new errorDialog();
    logoutObject = new Logout();
}